import { defineStore } from 'pinia'
import $S from '@/server/server.js'
const useFriend = defineStore('friend', {
	state:() => {
		return {
			feiendApplicationList: [] ,// 好友申请列表
			feiendApplicationTotal: 0, // 好友申请总数
			friendList: [], // 朋友列表数据
			checkedList: [] // 选择好友加入群聊的好友数据
		}
	},
	actions: {
		getFeiendApplicationList(page = 1,load = false){ // 获得好友申请列表
			$S.get(`/friend/list?page=${page}`,{},{token: true})
			.then(res => {
				this.feiendApplicationTotal = res.count
				// 组织数据格式
				const arr = res.rows.map((m) => {
					return {
						id: m.id,
						user_id: m.user_id,
						friend_id: m.friend_id,
						username: m.user.username,
						avatar: m.user.avatar,
						area: m.area,
						create_time: m.created_at.split('T')[0]
					}
				})
				if(load){
					this.feiendApplicationList = [...this.feiendApplicationList, ...arr]
				}else {
					this.feiendApplicationList = [...arr]
				}
				if(this.feiendApplicationTotal > 0){
					return uni.setTabBarBadge({
						index: 1,
						text: this.feiendApplicationTotal + ''
					})
				}
				uni.removeTabBarBadge({
					index:1
				})
			})
		},
		getFriendList(){ // 获得好友列表
			return new Promise((resolve,reject) => {
				$S.get('/friend',{},{token:true}).then(res =>{
					this.friendList = res.rows?.newList?.map((item) =>{
						return {
							initial: item.title,
							list:item.list
						}
					})
					resolve(this.friendList)
				})
			})
		},
		
	},
	getters: {
		checkedListLength(){ // 选择好友加入群聊的好友数据长度
			return this.checkedList.length
		}
	}
})

export default useFriend